
package br.com.leilao.dao;

import java.util.ArrayList;

import org.hibernate.Session;

import br.com.leilao.config.HibernateUtil;

public class GenericDAO<T> {
	private Session sessao;
	
	public boolean save(T objeto) {
		try {			
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			sessao.save(objeto);
			sessao.getTransaction().commit();
			return true;
		} catch (Exception e) {
			sessao.getTransaction().rollback();
			e.printStackTrace();
			return false;
		}finally{
			sessao.close();
		}
	}
	
	public boolean delete(T objeto) {
		try {			
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			sessao.delete(objeto);
			sessao.getTransaction().commit();
			return true;
		} catch (Exception e) {
			sessao.getTransaction().rollback();
			e.printStackTrace();
			return false;
		}finally{
			sessao.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public T find(Integer id, Class<?> classe) {
		try {			
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			T objeto = (T) sessao.get(classe, id);
			sessao.getTransaction().commit();
			return objeto;
		} catch (Exception e) {
			sessao.getTransaction().rollback();
			e.printStackTrace();
			return null;
		}finally{
			sessao.close();
		}
	}
	
	public boolean update(T objeto) {
		try {			
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			sessao.update(objeto);
			sessao.getTransaction().commit();
			return true;
		} catch (Exception e) {
			sessao.getTransaction().rollback();
			e.printStackTrace();
			return false;
		}finally{
			sessao.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<T> findAll(Class<?> classe) {
		try {			
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			
			String sql = "from "+ classe.getSimpleName();
			
			ArrayList<T> list = (ArrayList<T>)sessao.createQuery(sql).list();
			sessao.getTransaction().commit();
			return list;
		} catch (Exception e) {
			sessao.getTransaction().rollback();
			e.printStackTrace();
			return null;
		}finally{
			sessao.close();
		}
	}	
	
	@SuppressWarnings("unchecked")
	public ArrayList<T> findAllByAttr(Class<?> classe,String attr,String value) {
		try {			
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			
			String sql = "from "+ classe.getSimpleName()+" where "+attr+" like '%"+value+"%'";
			
			ArrayList<T> list = (ArrayList<T>)sessao.createQuery(sql).list();
			sessao.getTransaction().commit();
			return list;
		} catch (Exception e) {
			sessao.getTransaction().rollback();
			e.printStackTrace();
			return null;
		}finally{
			sessao.close();
		}
	}	
	
	public Session getSession(){
		if(this.sessao == null){
			return sessao = HibernateUtil.getSessionFactory().openSession();
		}
		return this.sessao;
	}
	
}
